package com.rp.configure.controller;


import com.alibaba.druid.util.HexBin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rp.common.result.ResultDTO;
import com.rp.common.utils.Selectway;
import com.rp.configure.entity.Attribute;
import com.rp.configure.entity.DeviceAttribute;
import com.rp.configure.entity.Svg;
import com.rp.configure.service.DeviceAttributeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author vicente
 * @since 2021-07-16
 */
@RestController
@RequestMapping("/device-attribute")
@Api(tags ="设备属性管理")
public class DeviceAttributeController {
    @Autowired
    private DeviceAttributeService deviceAttributeService;

    @Autowired
    private Selectway selectway;

    @ApiOperation(value = "aaa")
    @GetMapping(value = "/list_paaaa")
    public ResultDTO<IPage<DeviceAttribute>> sssdd() {
        byte[] bytes = HexBin.decode("02 02 01 02 20 0D".replace(" ", ""));
        deviceAttributeService.handleMessage("368a80f3c173a485ce36beb02e7e1f00",bytes);
        return ResultDTO.success();
    }
    @ApiOperation(value = "根据设备id选择属性")
    @GetMapping(value = "/list_deviceId")
    public ResultDTO<List<DeviceAttribute>> getByDeviceId(@RequestParam(value = "deviceId", required = false) String deviceId) {
        QueryWrapper<DeviceAttribute> queryWrapper = new QueryWrapper<DeviceAttribute>();
        queryWrapper.orderByDesc("create_time").eq("device_id",deviceId);
        return ResultDTO.success().data(deviceAttributeService.list(queryWrapper));
    }
    @ApiOperation(value = "分页列表")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    @GetMapping(value = "/list_page")
    public ResultDTO<IPage<DeviceAttribute>> listPage(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
                                            @RequestParam(value = "deviceId", required = false) String deviceId) {
        QueryWrapper<DeviceAttribute> queryWrapper = new QueryWrapper<DeviceAttribute>();
//        selectway.fields(queryWrapper, string,"gateway",null,null);
        queryWrapper.orderByDesc("create_time").eq("device_id",deviceId);
        return ResultDTO.success().data(deviceAttributeService.listPage(new Page<>(pageNum, pageSize), deviceId));
    }
    @ApiOperation(value = "查询设备属性状态，根据用户id,项目id")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    @GetMapping(value = "/list_state")
    public ResultDTO<IPage<DeviceAttribute>> listState(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
                                                      @RequestParam(value = "userId", required = false) String userId,
                                                       @RequestParam(value = "subjectId", required = false) String subjectId) {
        QueryWrapper<DeviceAttribute> queryWrapper = new QueryWrapper<DeviceAttribute>();
//        selectway.fields(queryWrapper, string,"gateway",null,null);
        queryWrapper.orderByDesc("create_time");
        return ResultDTO.success().data(deviceAttributeService.listState(new Page<>(pageNum, pageSize),userId,subjectId));
    }
    @ApiOperation(value = "根据项目和svg_id查询网关设备和属性")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    @GetMapping(value = "/get_subject")
    public ResultDTO<Svg> getBySubject(@RequestParam(value = "subjectId") String subjectId,
                                       @RequestParam(value = "svgId") String svgId
    ) {
        return ResultDTO.success().data(deviceAttributeService.getBySubject(subjectId,svgId));
    }
    @ApiOperation(value = "列表")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    @GetMapping(value = "/list")
    public ResultDTO<List<DeviceAttribute>> list(@RequestParam(value = "deviceId",required = false) String deviceId) {
        QueryWrapper<DeviceAttribute> queryWrapper = new QueryWrapper<DeviceAttribute>();
//        selectway.fields(queryWrapper, string,"gateway",null,null);
        queryWrapper.orderByDesc("create_time").eq("device_id",deviceId);
        return ResultDTO.success().data(deviceAttributeService.list(queryWrapper));
    }


    @PostMapping("/insert")
    @ApiOperation(value = "增加")
    @Transactional
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO insert(@RequestBody DeviceAttribute deviceAttribute) {

        return ResultDTO.tf(deviceAttributeService.save(deviceAttribute));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO update(@RequestBody DeviceAttribute deviceAttribute) {
        return ResultDTO.tf(deviceAttributeService.updateById(deviceAttribute));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO delete(@RequestBody @ApiParam(value = "id", required = true) String id) {
        return ResultDTO.tf(deviceAttributeService.removeById(id));
    }

    @DeleteMapping("/deleteAll")
    @ApiOperation(value = "批量删除")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO deleteAll(@RequestBody @ApiParam(value = "id(英文逗号隔开)", required = true) String ids) {
        return ResultDTO.tf(deviceAttributeService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
